package com.ilop.sthome.ui.adapter.detail;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.siterwell.familywellplus.R;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-04-06.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：插座联动列表适配器
 */
public class LinkageAdapter extends RecyclerView.Adapter<LinkageAdapter.ItemHolder> {

    private Context mContext;
    private List<AutomationBean> mList;

    public LinkageAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<AutomationBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_linkage_detail, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        AutomationBean bean = mList.get(i);
        List<DeviceInfoBean> inputList = bean.getInputList();
        List<DeviceInfoBean> outputList = bean.getOutputList();
        List<String> name = new ArrayList<>();
        for (DeviceInfoBean device : inputList) {
            name.add(mContext.getString(SmartDevice.getType(device.getDevice_type()).getTypeStrId()));
        }
        String outputType = outputList.get(0).getDevice_type();
        String outputName = mContext.getString(SmartDevice.getType(outputType).getTypeStrId());
        String mAutoName = StringUtils.join(name.toArray(), ",") + "-" + outputName;
        itemHolder.mTitle.setText(mAutoName);

        itemHolder.mTitle.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        itemHolder.mTitle.setSingleLine(true);
        itemHolder.mTitle.setSelected(true);
        itemHolder.mTitle.setFocusable(true);
        itemHolder.mTitle.setFocusableInTouchMode(true);
        int inputSize = inputList.size();
        switch (inputSize){
            case 1:
                itemHolder.input1.setImageResource(SmartDevice.getType(inputList.get(0).getDevice_type()).getDrawableResId());
                itemHolder.input2.setVisibility(View.GONE);
                itemHolder.input3.setVisibility(View.GONE);
                itemHolder.input4.setVisibility(View.GONE);
                break;
            case 2:
                itemHolder.input1.setImageResource(SmartDevice.getType(inputList.get(0).getDevice_type()).getDrawableResId());
                itemHolder.input2.setImageResource(SmartDevice.getType(inputList.get(1).getDevice_type()).getDrawableResId());
                itemHolder.input3.setVisibility(View.GONE);
                itemHolder.input4.setVisibility(View.GONE);
                break;
            case 3:
                itemHolder.input1.setImageResource(SmartDevice.getType(inputList.get(0).getDevice_type()).getDrawableResId());
                itemHolder.input2.setImageResource(SmartDevice.getType(inputList.get(1).getDevice_type()).getDrawableResId());
                itemHolder.input3.setImageResource(SmartDevice.getType(inputList.get(2).getDevice_type()).getDrawableResId());
                itemHolder.input4.setVisibility(View.GONE);
                break;
            default:
                itemHolder.input1.setImageResource(SmartDevice.getType(inputList.get(0).getDevice_type()).getDrawableResId());
                itemHolder.input2.setImageResource(SmartDevice.getType(inputList.get(1).getDevice_type()).getDrawableResId());
                itemHolder.input3.setImageResource(SmartDevice.getType(inputList.get(2).getDevice_type()).getDrawableResId());
                break;
        }
        itemHolder.output.setImageResource(SmartDevice.getType(outputType).getDrawableResId());
        itemHolder.itemView.setOnClickListener(view -> LiveDataBus.get().with(CommonId.LINKAGE_ON_CLICK).setValue(bean.getMid()));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.linkage_name)
        TextView mTitle;
        @BindView(R.id.input1)
        ImageView input1;
        @BindView(R.id.input2)
        ImageView input2;
        @BindView(R.id.input3)
        ImageView input3;
        @BindView(R.id.input4)
        ImageView input4;
        @BindView(R.id.output)
        ImageView output;
        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
